package game.editor;

import game.GameFieldPanel;
import game.GuessingPanel;
import game.MainController;
import game.MainModel;

import java.awt.BorderLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 * @author Fredrik
 *
 */
public class EditorMainFrame extends JFrame implements Observer {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3004431328008305682L;

	private GameFieldPanel frameWork;

	private ToolPanel frameTool;
	
	private GuessingPanel guessingPanel;

	private JPanel mainPanel = new JPanel();

	private JMenu menu = new JMenu("File");

	private JMenuItem menuNew = new JMenuItem("New");

	private JMenuItem menuExit = new JMenuItem("Exit");

	private JMenuItem menuOpen = new JMenuItem("Open");
	
	private JMenuItem menuSave = new JMenuItem("Save world");

	private JMenuBar menuBar = new JMenuBar();

	/**
	 * Creates a EditorMainFrame
	 * @param mainController
	 * @param model
	 */
	public EditorMainFrame(MainController mainController, MainModel model) {
		super();
		
		this.setTitle("Map editor");
		
		model.addObserver(this);
		
		mainController.setMainFrame(this);
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		this.menuNew.setActionCommand(MainController.MENU_ACTION_NEW);
		this.menuNew.addActionListener(mainController);
		this.menuExit.setActionCommand(MainController.MENU_ACTION_EXIT);
		this.menuExit.addActionListener(mainController);
		this.menuSave.setActionCommand(MainController.MENU_ACTION_SAVE);
		this.menuSave.addActionListener(mainController);
		this.menuOpen.setActionCommand(MainController.MENU_ACTION_OPEN_FILE);
		this.menuOpen.addActionListener(mainController);

		this.menu.add(this.menuNew);
		this.menu.add(this.menuOpen);
		this.menu.add(this.menuSave);
		this.menu.add(this.menuExit);
		this.menuBar.add(this.menu);

		this.setJMenuBar(this.menuBar);

		this.mainPanel.setLayout(new BorderLayout());
		//this.setSize(800, 600);
		
		this.guessingPanel = new GuessingPanel(model,mainController);
		
		this.frameWork = new GameFieldPanel(model);
		
		this.frameTool = new ToolPanel(mainController);
		
		this.frameWork.addMouseListener(mainController);
		this.frameWork.addMouseMotionListener(mainController);
		
		model.setEditingMode(true);
		

		this.mainPanel.add(this.frameWork, BorderLayout.CENTER);
		this.mainPanel.add(this.frameTool, BorderLayout.SOUTH);
		this.mainPanel.add(this.guessingPanel, BorderLayout.EAST);
		setResizable(false);
		this.add(this.mainPanel);
		pack();
	}

	/** (non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	public void update(Observable o, Object arg) {
		paintWorld();
	}

	
	/**
	 * Paints the world
	 */
	public void paintWorld(){
		
		repaint();
	}

	

}
